package com.zhanghe.study.spring4.beans.tx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * @author zh
 * @date 2021/1/6 11:57
 */
@Repository("userDao")
public class UserDaoImpl implements UserDao{

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void addAccount(int id, double money) {
        String sql = String.format("update user set account = account + %f where id = %d",money,id);
        jdbcTemplate.update(sql);
    }

    @Override
    public void subtractAccount(int id, double money) {
        String sql = String.format("update user set account = account - %f where id = %d",money,id);
        jdbcTemplate.update(sql);
    }

    @Override
    public double findAccountById(int id) {
        String sql = String.format("select account from user where id = %d",id);
        return jdbcTemplate.queryForObject(sql, Double.class);
    }
}
